(function(){
    function GetMachineReport(){
        this.init();
  
     
       
    }

    GetMachineReport.prototype = {
        constructor:GetMachineReport,
        init:function(){
            this.nodata = '<tr><td  colspan="8">暂无信息</td></tr>';
            var _this = this;
        
              $.datetimepicker.setLocale('ch');
				$('#endtime').datetimepicker({
                    format: 'Y-m-d',
                    value:new Date().dateadd("d", 1),
					maxDate: new Date(),
					onChangeDateTime:function(dp,$input){
						var limitStart = new Date($('#begintime').val().replace('-','/'));
						var nowStart = new Date($('#endtime').val().replace('-','/'));
						if(limitStart > nowStart){
							$('#endtime').val(limitStart.format("YY-M-D"))

						}
					},
					timepicker: false
                });
                
                $('#begintime').datetimepicker({
                    format: 'Y-m-d',
                    value:new Date().dateadd("d", -1),
                    maxDate: new Date(),
					onChangeDateTime:function(dp,$input){
                        var limitStart = new Date($('#endtime').val().replace('-','/'));
						var nowStart = new Date($('#begintime').val().replace('-','/'));
						if(limitStart < nowStart){
							$('#begintime').val(limitStart.format("YY-M-D"))

						}
						
					},
					timepicker: false
                });
                

                this.getReportList(1);
            
                $("#searchBtn").unbind("click").click(function(){
                     _this.getReportList(1);
                });

                $("#countBtn").unbind("click").click(function(){
                    _this.getCountReport();
               });



                $.extend({page:function(pn){
                    _this.getReportList(pn);
                }});






        },
        getReportList:function( pn ){
            var _this = this;
            var machineId = $("#machineNum").val();
            if( machineId )
            {
                machineId = "&siteid="+machineId;
            }
            var stime = $("#begintime").val() ;
            var etime = $("#endtime").val() ;
            $.ajax({
                type:"get",
                url:"/sys?fid=304"+machineId+"&stime="+stime+"&etime="+etime+"&ps=20&pn="+pn,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var data = info.data;
                        var count = info.count;
                        // state  -1撤单、0.未分配、1.已分配、2.已接单
                        var stateArr = ["未审核","已审核","已计奖"];
                        if( data.length )
                        {
                            var strArr = [];
                            data.forEach(function( value, key  ){
                                var siteid  = value.siteid ;
                                var outtime = value.outtime;
                                var tmoney = value.tmoney;
                                var mulity = value.mulity;
                                var sysmoney = value.sysmoney;
                                var wmoney = value.wmoney;
                                var money = value.money;
                                var cmoney = ( parseInt(sysmoney) - parseInt(wmoney) ).toFixed(2);
                               

                                var trClass =  key%2 == 0 ? "even" : "odd";
                                var html = '<tr class="'+trClass+'"  >\
                                                <td>'+siteid+'</td>\
                                                <td>'+outtime+'</td>\
                                                <td>'+tmoney+'</td>\
                                                <td>'+mulity+'</td>\
                                                <td>'+sysmoney+'</td>\
                                                <td>'+wmoney+'</td>\
                                                <td>'+money+'</td>\
                                                <td>'+cmoney+'</td>\
                                            </tr>';

                                    strArr.push( html );

                            });

                            $("#unticketList").html( strArr.join(""));
                          
                            
                            // 分页
                            var pn = count.pn;
                            var tp = count.tp;
                            var tr = count.tr;
                            $("#countpages").html( tp );
                            $("#countnums").html( tr );
        
                            var pageHtml = getPages(pn,tp, "$.page");

                            $("#paginationSel").html(pageHtml);



                        }
                        else
                        {
                            $("#unticketList").html(_this.nodata);

                        }

                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }
                    }


                }
            });


        },
        // 汇总
        getCountReport:function(){
            $("#countpages").html("");
            $("#countnums").html( "" );
            $("#paginationSel").html("");
            var _this = this;
            var machineId = $("#machineNum").val();
            if( machineId )
            {
                machineId = "&siteid="+machineId;
            }
            var stime = $("#begintime").val(); 
            var etime = $("#endtime").val() ;
            $.ajax({
                type:"get",
                url:"/sys?fid=305"+machineId+"&stime="+stime+"&etime="+etime,
                dataType:"json",
                success:function( info ){
                    var code = info.code;
                    var desc = info.desc;
                    if( code == 1 )
                    {
                        var value = info.data;
                         // state  -1撤单、0.未分配、1.已分配、2.已接单
                         var stateArr = ["未审核","已审核","已计奖"];
                         if( value )
                         {

                            var outtime = value.outtime;
                            var tmoney = value.tmoney;
                            var mulity = value.mulity;
                            var sysmoney = value.sysmoney;
                            var wmoney = value.wmoney;
                            var money = value.money;
                            var cmoney = ( parseInt(sysmoney) - parseInt(wmoney) ).toFixed(2);

                            var html = '<tr  >\
                                            <td>'+machineId+'</td>\
                                            <td>'+outtime+'</td>\
                                            <td>'+tmoney+'</td>\
                                            <td>'+mulity+'</td>\
                                            <td>'+sysmoney+'</td>\
                                            <td>'+wmoney+'</td>\
                                            <td>'+money+'</td>\
                                            <td>'+cmoney+'</td>\
                                        </tr>';

                           $("#unticketList").html( html);


                         }
                         else
                         {
                            $("#unticketList").html(_this.nodata);

                         }


                    }
                    else
                    {
                        if( code == 2 )
                        {
                            location.href = "login.html";

                        }
                        else
                        {
                            $("#tipsContent").html( desc );
                            $("#loginoutModal").modal();

                        }

                    }


                }

            });


        }
   
      
      
    };





  new GetMachineReport();

}());